<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close @closed="$emit('closed')"
			   align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="100" ref="dialogForm">
			<el-row :gutter="1">
				<el-col :span="6">
					<el-form-item label="关联采购单" prop="purchase_id">
						<div style="display:flex;width:100%">
							<el-input v-model="purchase_code" placeholder="请选择采购单" readonly @click="checkProduct"></el-input>
							<el-button type="info" icon="el-icon-Close" @click="clearProduct"></el-button>
						</div>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="供应商" prop="supplier_id">
						<el-select v-model="form.supplier_id" placeholder="请选择" filterable clearable :disabled="form.purchase_id != ''">
							<el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="退货单号" prop="return_code">
						<el-input v-model="form.return_code" placeholder="请输入退货单号" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="退货日期" prop="open_date">
						<el-date-picker v-model="form.open_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%"/>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="退货仓库" prop="ware_id">
						<el-select v-model="ware_id" filterable style="width: 100%" placeholder="请选择仓库" @change="wareChange" clearable>
							<el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"/>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-col :span="24">
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
				</el-form-item>
			</el-col>
			<el-form-item label="退货清单" prop="detail">
				<el-button type="primary" size="small" icon="el-icon-plus" @click="checkGoods" v-if="form.purchase_id == ''">选择产品</el-button>
				<sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="400" hideAdd :hideDelete="form.purchase_id != ''">
					<el-table-column label="产品名称" align="center">
						<template #default="scope">
							{{ scope.row.goods_name }}
						</template>
					</el-table-column>
					<el-table-column label="产品编号" align="center">
						<template #default="scope">
							{{ scope.row.goods_code }}
						</template>
					</el-table-column>
					<el-table-column label="单位" align="center" prop="unit_name"></el-table-column>
					<el-table-column label="退货数量" prop="number" align="center">
						<template #default="scope">
							<el-input v-model="scope.row.number" placeholder="退货数量" @focus="onFocus($event)"></el-input>
						</template>
					</el-table-column>
					<!-- <el-table-column label="单价" prop="price" align="center">
						<template #default="scope">
							<el-input v-model="scope.row.price" placeholder="单价" @input="totalCompute" @focus="onFocus($event)"></el-input>
						</template>
					</el-table-column>
					<el-table-column label="小计" prop="total_price" align="center">
						<template #default="scope">
							<el-input v-model="scope.row.total_price" placeholder="小计" readonly></el-input>
						</template>
					</el-table-column> -->
					<el-table-column label="退货仓库" align="center">
						<template #default="scope">
							<el-select v-model="scope.row.ware_id" filterable style="width: 100%" placeholder="仓库">
								<el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"/>
							</el-select>
						</template>
					</el-table-column>
					<!-- <el-table-column label="备注" prop="remark" align="center">
						<template #default="scope">
							<el-input v-model="scope.row.remark" placeholder="备注"></el-input>
						</template>
					</el-table-column> -->
				</sc-form-table>
			</el-form-item>
			<el-form-item label="附件" prop="enclosure">
				<sc-upload-file v-model="form.enclosure" :limit="1">
					<el-button type="primary" icon="el-icon-upload">选择文件</el-button>
				</sc-upload-file>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit(0)">保 存</el-button>
			<el-button v-if="mode == 'add'" type="primary" :loading="isSaveing" @click="submit(1)">保存并审核</el-button>
		</template>
	</el-dialog>
	<select-order v-if="dialog.order" ref="orderDialog" @success="orderUpdate" @closed="dialog.order=false"></select-order>
	<select-inorder v-if="dialog.inorder" ref="inorderDialog" @success="inorderUpdate" @closed="dialog.inorder=false"></select-inorder>
	<select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsUpdate" @closed="dialog.goods=false" :query="{system_disable:0}"></select-goods>
</template>

<script>
import selectGoods from '../../goods/info/selectGoods.vue';
import SelectOrder from '../order/selectOrder.vue';
import SelectInorder from '../inhouse/selectInorder.vue';

export default {
	components: {SelectOrder, selectGoods, SelectInorder},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看',
			},
			visible: false,
			isSaveing: false,
			form: {
				id: "",
				purchase_id: "",
				supplier_id: "",
				return_code: "",
				open_date: this.moment().format('YYYY-MM-DD'),
				remark: "",
				enclosure: "",
				detail: [],
				is_check: 0,
			},
			addTemplate: {},
			dialog: {
				order: false,
				inorder: false,
				goods: false,
			},
			adminList: [],
			wareList: [],
			purchase_code: "",
			ware_id: "",
			applyList: [],
			//验证规则
			rules: {
				// supplier_id: [{required: true, message: '请选择供应商'}],
				// return_code: [{required: true, message: '请输入退货单号'}],
				open_date: [{required: true, message: '请选择退货日期'}],
			},
		}
	},
	mounted() {
		this.getApply();
		this.getUser()
		this.getWare()
		this.getGetCode()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit(e) {
			this.form.is_check = e
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.procure.outorder.edit.post(this.form);
					} else {
						res = await this.$API.procure.outorder.add.post(this.form);
					}
					this.isSaveing = false;
					if (res != '') {
						if (res.code == 1) {
							this.visible = false;
							this.$emit('success', this.form, this.mode)
							this.$message.success("操作成功")
							// this.$emit('success', this.form, this.mode)
							// this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.procure.outorder.detail.get({id: data.id});
			this.form.purchase_id = res.data.purchase_id
			this.purchase_code = data.purchase && data.purchase.code
			this.form.supplier_id = res.data.supplier_id
			this.form.collect_staff = res.data.collect_staff
			this.form.purchase_staff = res.data.purchase_staff
			this.form.open_date = res.data.open_date
			this.form.entry_date = res.data.entry_date
			this.form.remark = res.data.remark
			this.form.enclosure = res.data.enclosure
			let array = []
			res.data.detail.map(item => {
				array.push({
					id: item.id,
					goods_id: item.goods_id,
					goods_name: item.goods && item.goods.goods_name,
					unit_name: item.goods && item.goods.unit && item.goods.unit.unit_name,
					goods_code: item.goods && item.goods.goods_code,
					number: item.number,
					price: item.price,
					total_price: item.total_price,
					// remark:item.remark,
					ware_id: item.ware_id,
				})
			})
			this.form.detail = array
		},
		// 选择采购单
		checkProduct() {
			this.dialog.order = true
			this.$nextTick(() => {
				this.$refs.orderDialog.open('add')
			})
		},
		// 选择采购单返回
		async orderUpdate(data) {
			this.form.purchase_id = data.id
			this.form.supplier_id = data.apply_id
			this.purchase_code = data.code
			var res = await this.$API.procure.order.detail.get({id: data.id});
			let array = []
			res.data.detail.map(item => {
				array.push({
					goods_id: item.goods_id,
					goods_name: item.goods && item.goods.goods_name,
					unit_name: item.goods && item.goods.unit && item.goods.unit.unit_name,
					goods_code: item.goods && item.goods.goods_code,
					number: item.number,
					price: item.price,
					total_price: item.total_price,
					ware_id: this.ware_id ? this.ware_id : '',
					// remark:item.remark,
				})
			})
			this.form.detail = array
		},
		clearProduct() {
			this.form.purchase_id = ""
			this.form.supplier_id = ""
			this.purchase_code = ""
			this.form.detail = []
		},
		// 选择产品
		checkGoods() {
			this.dialog.goods = true
			this.$nextTick(() => {
				this.$refs.goodsDialog.open('add')
			})
		},
		// 选择产品返回
		goodsUpdate(data) {
			let array = []
			data.map((item) => {
				array.push({
					goods_id: item.id,
					goods_name: item.goods_name,
					unit_name: item.unit && item.unit.unit_name,
					goods_code: item.goods_code,
					ware_id: this.ware_id ? this.ware_id : '',
				})
			})
			let existingIds = this.form.detail.map(item => item.goods_id);
			for (let i = array.length - 1; i >= 0; i--) {
				if (existingIds.includes(array[i].goods_id)) {
					array.splice(i, 1);
				}
			}
			this.form.detail = [...this.form.detail, ...array]
		},
		// 获取收货人
		async getUser() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;

		},
		// 供应商列表
		async getApply() {
			var res = await this.$API.apply.apply.all.get();
			this.applyList = res.data
		},
		async getGetCode() {
			var res = await this.$API.procure.outorder.code.get();
			this.form.return_code = res.data.code
		},
		// 获取仓库
		async getWare() {
			var res = await this.$API.goods.ware.all.get();
			this.wareList = res.data;
			if (this.mode == 'add') {
				res.data.map(item => {
					if (item.is_default == 1) {
						this.ware_id = item.id
					}
				})
			}
		},
		async getLocationList(row) {
			const res = await this.$API.goods.ware.position.post({ware_id: row.ware_id})
			row.locationList = res.data
		},
		onFocus(event) {
			event.target.select();
		},
		totalCompute() {
			this.form.detail.map(item => {
				if (item.price && item.number) {
					item.total_price = (Number(item.number) * Number(item.price)).toFixed(2)
				}
			})
		},
		wareChange(e) {
			this.form.detail.map(item => item.ware_id = e)
		},
	}
}
</script>

<style>
</style>
